home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / demostuf / pcx256.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-07-25  |  4.1 KB  |  237 lines

  1. UNIT PCX256;
  2. {
  3.   Converts a PCX 320*200 pixels picture in 256 colours to
  4.   a raw picture
  5.  
  6.   THIS PROGRAM WAS CODED BY BJARKE VIKS0E.
  7.   YOU ARE FREE TO DO WHATEVER YOU WANT WITH THIS PIECE OF CODE.
  8.   E-MAIL ME AT: dat92230@rix02.lyngbyes.dk IN 1994 FOR CHAT AND CODE.
  9. }
  10.  
  11. INTERFACE
  12.  
  13. uses
  14.     DEMOINIT;
  15.  
  16. type
  17.     filenamestring = string[40];
  18.  
  19. var
  20.     cmap : array[1..256*3] of byte;
  21.  
  22.  
  23. procedure LoadPix(buffer : pScreen; filename : filenamestring);
  24. procedure SetCMAP;
  25. procedure Copy2Screen(v : pScreen; s : pScreen);
  26. procedure Copy2TweakScreen(v : pScreen; s : pScreen);
  27. procedure MakeTweak(scr1,scr2 : pScreen);
  28. procedure FadeCMAP(faktor : integer);
  29.  
  30. (*--------------------------------------*)
  31.  
  32. IMPLEMENTATION
  33.  
  34. type
  35.     pBuffer = ^buffertype;
  36.     buffertype = array[1..64000] of byte;
  37.  
  38.     pHeader = ^HeaderType;
  39.     HeaderType = RECORD
  40.         id : byte;
  41.         ver : byte;
  42.         compressed : boolean;
  43.         bitpixel : byte;
  44.         minx,miny,maxx,maxy : word;
  45.         xsize,ysize : word;
  46.         palette : array[1..48] of byte;
  47.         unknown : byte;
  48.         depth : byte;
  49.         width : word;
  50.         palette_type : word;
  51.         filler : array[1..58] of byte;
  52.     end;
  53.  
  54.  
  55. procedure extractCMAP(v : pBuffer; size : longint);
  56. var
  57.     r,g,b : byte;
  58.     i,j,k : word;
  59. begin
  60.     i:=size-(256*3)+1;
  61.     k:=1;
  62.     for j:=1 to 256 do begin
  63.         r:=v^[i] div 4; g:=v^[i+1] div 4; b:=v^[i+2] div 4;
  64.         inc(i,3);
  65.         cmap[k]:=r; cmap[k+1]:=g; cmap[k+2]:=b;
  66.         inc(k,3);
  67.     end;
  68. end;
  69.  
  70.  
  71. procedure DecompressPCX(p : pScreen; v : pBuffer; h : pHeader);
  72. var
  73.     xsize, ysize : integer;
  74. begin
  75.  
  76.     xsize:=h^.xsize;
  77.     for ysize:=1 to h^.ysize do
  78.     asm
  79.         push    ds
  80.         lds    si,v
  81.         les    di,p
  82. @bigloop:
  83.         xor    bx,bx
  84.         xor    cx,cx
  85.         mov    dl,$C0
  86.         mov    dh,$3F
  87. @loop:
  88.         lodsb
  89.         mov    cl,1
  90.         mov    ah,al
  91.         and    ah,dl
  92.         cmp    ah,dl
  93.         jne    @copy
  94.         and    al,dh
  95.         mov    cl,al
  96.         lodsb
  97. @copy:
  98.         add    bx,cx
  99.         rep stosb
  100.         cmp    bx,xsize
  101.         jb        @loop
  102.         pop    ds
  103.         mov    WORD PTR v,si
  104.         mov    ax,xsize
  105.         add    WORD PTR p,ax
  106.     end;
  107. end;
  108.  
  109. procedure ConvertPCX(p : pScreen; v : pBuffer; size : longint);
  110. var
  111.     h : pHeader;
  112.     i : longint;
  113. begin
  114.     h := pHeader(v);
  115.     with h^ do begin
  116.         if (id<>$0A) OR (NOT compressed) then exit;
  117.         if (bitpixel<>8) OR (depth<>$01) then exit;
  118.     end;
  119.     extractCMAP(v, size);
  120.     if (h^.ver=$05) then decompressPCX(p,@v^[129],h);
  121. end;
  122.  
  123.  
  124. procedure LoadPix(buffer : pScreen; filename : filenamestring);
  125. var
  126.     pFileMem: pBuffer;
  127.     FileHandle : file;
  128.     size : longint;
  129. begin
  130.     {$I-}
  131.     Assign(FileHandle, filename);
  132.     Reset(FileHandle, 1);
  133.     {$I+}
  134.     if (IOresult<>0) then halt;
  135.     size := filesize(FileHandle);
  136.     GetMem(pFileMem, size);
  137.     BlockRead(FileHandle, pFileMem^, size);
  138.     Close(FileHandle);
  139.     ConvertPCX(buffer, pFileMem, size);
  140.     FreeMem(pFileMem, size);
  141. end;
  142.  
  143.  
  144. (*--------------------------------------*)
  145.  
  146. procedure SetCMAP;
  147. var
  148.     i,j : integer;
  149. begin
  150.     j:=1;
  151.     for i:=0 to 255 do begin
  152.         SetRGB(i,cmap[j],cmap[j+1],cmap[j+2]);
  153.         inc(j,3);
  154.     end;
  155. end;
  156.  
  157. procedure Copy2Screen(v : pScreen; s : pScreen); assembler;
  158. asm
  159.     push    ds
  160.     lds    si,v
  161.     les    di,s
  162.     cld
  163.     mov    cx,320*200/2
  164.     rep movsw
  165.     pop    ds
  166. end;
  167.  
  168.  
  169. procedure Copy2TweakScreen(v : pScreen; s : pScreen);
  170. const
  171.     size = 80*200;
  172.     procedure CopyPlane(v : pScreen; s : pScreen); assembler;
  173.     asm
  174.         push    ds
  175.         lds    si,v
  176.         les    di,s
  177.         cld
  178.         mov    cx,size/2
  179.         rep movsw
  180.         pop    ds
  181.     end;
  182. begin
  183.     SetBitplanes(1);
  184.     CopyPlane(@v^[0],s);
  185.     SetBitplanes(2);
  186.     CopyPlane(@v^[size+0],s);
  187.     SetBitplanes(4);
  188.     CopyPlane(@v^[size*2+0],s);
  189.     SetBitplanes(8);
  190.     CopyPlane(@v^[size*3+0],s);
  191. end;
  192.  
  193.  
  194. procedure MakeTweak(scr1,scr2 : pScreen);
  195. var
  196.     i,scroffset : integer;
  197. begin
  198.     scroffset:=0;
  199.     for i:=0 to 3 do begin
  200.         SetBitplanes(1 shl i);
  201.         asm
  202.             push    ds
  203.             lds    si,scr1
  204.             les    di,scr2
  205.             add    si,i
  206.             add    di,scroffset
  207.             mov    cx,80*200
  208.             mov    dx,4
  209.             cld
  210. @loop1:    mov    al,[si]
  211.             stosb
  212.             add    si,dx
  213.             loop    @loop1
  214.             pop    ds
  215.         end;
  216.         inc(scroffset,80*200);
  217.     end;
  218. end;
  219.  
  220. procedure FadeCMAP(faktor : integer);
  221. var
  222.     i,j : integer;
  223. begin
  224.     VBLANK;
  225.     j:=1;
  226.     for i:=0 to 255 do begin
  227.         SetRGB(i,
  228.                 longmul(cmap[j],faktor) shr 8,
  229.                 longmul(cmap[j+1],faktor) shr 8,
  230.                 longmul(cmap[j+2],faktor) shr 8);
  231.         inc(j,3);
  232.     end;
  233. end;
  234.  
  235.  
  236. end.
  237.